package com.example.nonacademic_education.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.nonacademic_education.constant.Constant;
import com.example.nonacademic_education.constant.SingleFactory;
import com.example.nonacademic_education.data.request.Graduate;
import com.example.nonacademic_education.data.request.ProjectExcelVo;
import com.example.nonacademic_education.data.request.StudentExcelVo;
import com.example.nonacademic_education.data.request.StudentList;
import com.example.nonacademic_education.data.response.StudentDto;
import com.example.nonacademic_education.entity.Student;
import com.example.nonacademic_education.service.impl.StudentServiceImpl;
import com.example.nonacademic_education.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:yanghua
 * @Date:2023/4/2 11:17
 */
@Slf4j
@RestController
@RequestMapping("student")
public class StudentController {
    @Resource
    private StudentServiceImpl studentService;
    @PostMapping("add")
    @SaCheckPermission("student.add")
    public R addStudent(@RequestBody StudentDto student){
        return studentService.addStudent(student);
    }

    //@PutMapping("graduate/{identityCardId}")
    public R graduateStudent(@RequestBody String certificateNo, @PathVariable String identityCardId){
        return studentService.toGraduate(certificateNo,identityCardId);
    }
    @PostMapping("getList")
    @SaCheckPermission("student.query")
    public R getListStudent(@RequestBody StudentList studentList){
        return studentService.getStudentList(studentList);
    }

    @DeleteMapping("deleteById/{id}")
    @SaCheckPermission("student.delete")
    public R deleteById(@PathVariable Long id){
        return studentService.deleteById(id);
    }
    @PutMapping("update")
    @SaCheckPermission("student.update")
    public R updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    @GetMapping("getExcelTemplate")
    @SaCheckPermission("student.add")
    public void getExcelTemplate(HttpServletResponse response) throws IOException {
        studentService.getExcelTemplate(response);
    }
    @PostMapping("importStudents")
    @SaCheckPermission("student.add")
    public R importStudents(@RequestBody MultipartFile file,Long classId,Long enrollmentTime,Long graduateTime,Integer item,String trainingInfo,String creationTime){
        if (item==1){
            return studentService.importStudentsGraduated(file,classId,enrollmentTime,graduateTime,trainingInfo,Long.parseLong(creationTime));
        }else {
            return studentService.importStudents(file,classId,enrollmentTime,graduateTime);
        }
    }
    @PutMapping("graduateStudents")
    @SaCheckPermission("class.graduate")
    public R graduateStudents(@RequestBody Graduate graduate){
        return studentService.graduateStudents(graduate);
    }
    @GetMapping("getGraduateInfo")
    @SaCheckPermission("student.export")
    public R getGraduateInfo(Long classId){
        return studentService.getGraduateInfo(classId);
    }

    @PostMapping("exportStatisticsExcel")
    @SaCheckPermission("student.excel")
    public void exportStatisticsExcel(@RequestBody StudentExcelVo studentExcelVo,HttpServletResponse response) throws IOException {
        //http://localhost:39001/student/exportStatisticsExcel
        //StudentExcelVo studentExcelVo=new StudentExcelVo();
        studentService.exportStatisticsExcel(studentExcelVo,response);
    }
//    @PutMapping("resetStuPassword/{username}")
//    @SaCheckRole(Constant.NORMAL_ADMIN)
//    public R resetStuPassword(@PathVariable String username) {
//        return studentService.resetStuPassword();
//    }

    @PutMapping("disableAccount")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R disableAccount(String identityCardId) {
        return studentService.enableAccount(identityCardId,0);
    }
    @PutMapping("enableAccount")
    @SaCheckRole(Constant.NORMAL_ADMIN)
    public R enableAccount(String identityCardId) {
        return studentService.enableAccount(identityCardId,1);
    }
}
